/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.cakerun.script;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class QiyeWeixinScript {
    private static final String CORPID = "wwe7d8d898c993eda6";
    private static final String CORPSECRET = "cDORZXJOCGd0Xp9mrXFhjhmxGUAwM7vNmMQ4kAAh7Kk";
    // 获取Access_token url
    private static final String ACCESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s";
    private static final String CHECKINDATA_URL = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata";
    private static final String GET_DEPT_LIST_URL = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=%s";
    private static final String GET_USER_BY_DEPT_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelis?access_token=%s";

    public String getAccessToken() {
        HttpRequest httpRequest = HttpRequest.get(String.format(ACCESS_TOKEN_URL, CORPID, CORPSECRET));
        try (HttpResponse execute = httpRequest.execute();) {
            String body = execute.body();
            JSONObject jsonObject = JSON.parseObject(body);
            return jsonObject.getString("access_token");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    public void getAllDept(String accessToken) {
        HttpRequest httpRequest = HttpRequest.get(String.format(GET_DEPT_LIST_URL, accessToken));
        try (HttpResponse execute = httpRequest.execute();) {
            String body = execute.body();
            JSONObject jsonObject = JSON.parseObject(body);
        }
    }

    public void getcheckindata(String accessToken, List<String> userIds) {
        JSONObject jsonObject = JSONObject.of("opencheckindatatype", "3", "starttime", System
            .currentTimeMillis(), "endtime", System.currentTimeMillis(), "useridlist", userIds);
        HttpRequest httpRequest = HttpRequest.post(CHECKINDATA_URL + "?access_token=" + accessToken)
            .body(jsonObject.toJSONString());
        try (HttpResponse execute = httpRequest.execute();) {
            String body = execute.body();
            JSONObject bodyJson = JSON.parseObject(body);
            Integer errcode = bodyJson.getInteger("errcode");
            if (errcode != null && errcode == 0) {
                JSONArray checkindata = bodyJson.getJSONArray("checkindata");
                System.out.println(checkindata);
            }
        }
    }

    public static void main(String[] args) {
        QiyeWeixinScript qiyeWeixinScript = new QiyeWeixinScript();
        String accessToken = qiyeWeixinScript.getAccessToken();
        qiyeWeixinScript.getAllDept(accessToken);
    }
}
